package net.shopxx.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import net.shopxx.dao.RoleDao;
import net.shopxx.entity.Admin;
import net.shopxx.entity.Role;
import net.shopxx.service.RoleService;
import net.shopxx.util.SpringUtil;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.security.intercept.web.FilterInvocationDefinitionSource;
import org.springframework.security.intercept.web.FilterSecurityInterceptor;
import org.springframework.stereotype.Service;
import org.springmodules.cache.annotations.CacheFlush;
import org.springmodules.cache.annotations.Cacheable;

/**
 * Service实现类 - 角色
 */

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role, String> implements RoleService {
	
	@Resource
	RoleDao roleDao;

	@Resource
	public void setBaseDao(RoleDao roleDao) {
		super.setBaseDao(roleDao);
	}
	
	// 重写方法，删除时刷新SpringSecurity权限信息
	@Override
	@CacheFlush(modelId = "flushing")
	public void delete(Role role) {
		roleDao.delete(role);
		roleDao.flush();
		flushSpringSecurity();
	}

	// 重写方法，删除时刷新SpringSecurity权限信息
	@Override
	@CacheFlush(modelId = "flushing")
	public void delete(String id) {
		Role role = roleDao.load(id);
		this.delete(role);
	}

	// 重写方法，删除时刷新SpringSecurity权限信息
	@Override
	@CacheFlush(modelId = "flushing")
	public void delete(String[] ids) {
		for (String id : ids) {
			Role role = roleDao.load(id);
			roleDao.delete(role);
		}
		roleDao.flush();
		flushSpringSecurity();
	}

	// 重写方法，保存时刷新SpringSecurity权限信息
	@Override
	@CacheFlush(modelId = "flushing")
	public String save(Role role) {
		String id = roleDao.save(role);
		roleDao.flush();
		roleDao.clear();
		flushSpringSecurity();
		return id;
	}

	// 重写方法，更新时刷新SpringSecurity权限信息
	@Override
	@CacheFlush(modelId = "flushing")
	public void update(Role role) {
		roleDao.update(role);
		roleDao.flush();
		flushSpringSecurity();
	}
	
	// 刷新SpringSecurity权限信息
	private void flushSpringSecurity() {
		try {
			FactoryBean factoryBean = (FactoryBean)SpringUtil.getBean("&adminSecurityDefinitionSource");
			FilterInvocationDefinitionSource filterInvocationDefinitionSource = (FilterInvocationDefinitionSource) factoryBean.getObject();
		    FilterSecurityInterceptor filterSecurityInterceptor = (FilterSecurityInterceptor) SpringUtil.getBean("filterSecurityInterceptor");
		    filterSecurityInterceptor.setObjectDefinitionSource(filterInvocationDefinitionSource);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Cacheable(modelId = "caching")
	public List<Role> getRootRoleList() {
		List<Role> rootRoleList = roleDao.getRootRoleList();
		if (rootRoleList != null) {
			for (Role rootRole : rootRoleList) {
				Hibernate.initialize(rootRole);
			}
		}
		return rootRoleList;
	}
	
	@Cacheable(modelId = "caching")
	public List<Role> getParentRoleList(Role role) {
		List<Role> parentRoleList = roleDao.getParentRoleList(role);
		if (parentRoleList != null) {
			for (Role parentRole : parentRoleList) {
				Hibernate.initialize(parentRole);
			}
		}
		return parentRoleList;
	}
	@Cacheable(modelId = "caching")
	public List<Role> getChildrenRoleList(Role role) {
		List<Role> childrenRoleList = roleDao.getChildrenRoleList(role);
		if (childrenRoleList != null) {
			for (Role childrenRole : childrenRoleList) {
				Hibernate.initialize(childrenRole);
			}
		}
		return childrenRoleList;
	}
	
	public List<Role> getRolePathList(Role role) {
		List<Role> rolePathList = new ArrayList<Role>();
		rolePathList.addAll(this.getParentRoleList(role));
		rolePathList.add(role);
		return rolePathList;
	}
	
	public List<Role> getParentRoleList(Admin admin) {
		Set<Role> roleSet = admin.getRoleSet();
		List<Role> roleList = new ArrayList<Role>();
		for(Role role:roleSet){
		roleList.addAll(this.getParentRoleList(role));
		roleList.add(role);
		}
		return roleList;
	}

	public List<Role> getRolePathList(Admin admin) {
		Set<Role> roleSet = admin.getRoleSet();
		List<Role> roleList = new ArrayList<Role>();
		for(Role role:roleSet){
		roleList.addAll(this.getParentRoleList(role));
		roleList.add(role);
		}
		return roleList;
	}

	public List<Role> getChildrenRoleList(Admin admin) {
		Set<Role> roleSet = admin.getRoleSet();
		List<Role> roleList = null;
		for(Role role:roleSet){
		 roleList = getChildrenRoleList(role);
		if (roleList == null) {
			roleList = new ArrayList<Role>();
		}
		roleList.add(role);
		}
		return roleList;
	}
	
	@Cacheable(modelId = "caching")
	public List<Role> getRoleTreeList() {
		List<Role> allRoleList = this.getAll();
		return recursivRoleTreeList(allRoleList, null, null);
	}
	
	// 递归父类排序分类树
	private List<Role> recursivRoleTreeList(List<Role> allRoleList, Role p, List<Role> temp) {
		if (temp == null) {
			temp = new ArrayList<Role>();
		}
		for (Role role : allRoleList) {
			Role parent = role.getParent();
			if ((p == null && parent == null) || (role != null && parent == p)) {
				temp.add(role);
				if (role.getChildren() != null && role.getChildren().size() > 0) {
					recursivRoleTreeList(allRoleList, role, temp);
				}
			}
		}
		return temp;
	}

	@Override
	@Cacheable(modelId = "caching")
	public List<Role> getAll() {
		List<Role> allRole = roleDao.getAll();
		if (allRole != null) {
			for (Role role : allRole) {
				Hibernate.initialize(role);
			}
		}
		return allRole;
	}

}